package com.moon;

import cn.yueliangbaba.R;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.loopj.android.http.RequestParams;
import com.sys.DemoApp;
import com.umeng.message.UmengRegistrar;
import com.util.ImageLoaderHelper;
import com.util.MgqDataHandler;
import com.util.MgqRestClient;
import com.util.PreferencesUtils;
import com.util.ToastUtil;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class SettingsActivity extends BaseActivity implements OnClickListener{
	int mSilent;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_settings);
		
		((TextView) findViewById(R.id.center)).setText("系统设置");
		findViewById(R.id.left).setOnClickListener(this);
		findViewById(R.id.btn0).setOnClickListener(this);
		findViewById(R.id.btn1).setOnClickListener(this);
		findViewById(R.id.btn2).setOnClickListener(this);
		
		JSONObject member = JSON.parseObject(PreferencesUtils.getString(this, "member"));
		mSilent = member.getIntValue("is_silent");
		if(mSilent == 1) ((TextView)findViewById(R.id.btn2)).setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.check_win_focus, 0);
	}

	@Override
	public void onClick(View arg0) {
		if(arg0.getId() == R.id.btn0)
		{
			ImageLoaderHelper.clearDataCache();
			ToastUtil.showToastWithAlertPic("清除成功");
		}
		else if(R.id.btn1 == arg0.getId())
		{
			MgqDataHandler loginHandler = new MgqDataHandler(this, true, false) {
				@Override
				public void onSuccess(String response) {
					JSONObject ruser = JSON.parseObject(response);
					if (ruser.getString("statusCode").equals("0")) {
						ToastUtil.showToastWithAlertPic("清除成功");
						
						MainPageActivity.mMainPageActivity.autoRefresh(0);
					}
					else 
					{
						ToastUtil.showToastWithAlertPic(ruser.getString("message"));
					}
				}
				
				@Override
				public void onFailure(Throwable ble) {
					super.onFailure(ble);
					ToastUtil.showToastWithAlertPic("网络状况不佳，请稍后重试...");
				}
			};
			
			String ids = "";
			for(Integer id : ((DemoApp)getApplicationContext()).mClassesSelected)
			{
				if(ids.length() > 0) ids += ",";
				ids += id;
			}
			
			RequestParams params = new RequestParams();
			params.put("id", PreferencesUtils.getString(this, "userId"));
			params.put("class_id", ids);
			MgqRestClient.get("send_feedback_all", params, loginHandler);
		}
		else if(R.id.btn2 == arg0.getId())
		{
			MgqDataHandler loginHandler = new MgqDataHandler(this, true, false) {
				@Override
				public void onSuccess(String response) {
					JSONObject ruser = JSON.parseObject(response);
					if (ruser.getString("statusCode").equals("0")) {
						sendRequest();
					}
					else 
					{
						ToastUtil.showToastWithAlertPic(ruser.getString("message"));
					}
				}
				
				@Override
				public void onFailure(Throwable ble) {
					super.onFailure(ble);
					ToastUtil.showToastWithAlertPic("网络状况不佳，请稍后重试...");
				}
			};
			
			RequestParams params = new RequestParams();
			params.put("id", PreferencesUtils.getString(this, "userId"));
			if(mSilent == 0) params.put("is_silent", "1");
			else params.put("is_silent", "0");
			MgqRestClient.get("set_silent", params, loginHandler);
		}
		else if(R.id.left == arg0.getId())
		{
			finish();
		}
	}
	
	private void sendRequest() {
		MgqDataHandler loginHandler = new MgqDataHandler(this, true, false) {
			@Override
			public void onSuccess(String response) {
				JSONObject ruser = JSON.parseObject(response);
				if (ruser.getString("statusCode").equals("0")) {
					JSONObject member = ruser.getJSONObject("member");
					
					PreferencesUtils.putString(SettingsActivity.this, "member", JSON.toJSONString(member));
					PreferencesUtils.putString(SettingsActivity.this, "classes", JSON.toJSONString(member.getJSONArray("classes")));
					PreferencesUtils.putString(SettingsActivity.this, "children", JSON.toJSONString(member.getJSONArray("children")));
					
					mSilent = member.getIntValue("is_silent");
					if(mSilent == 1) ((TextView)findViewById(R.id.btn2)).setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.check_win_focus, 0);
					else ((TextView)findViewById(R.id.btn2)).setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.check_win, 0);
				}
				else
				{
					ToastUtil.showToastWithAlertPic(ruser.getString("message"));
				}
			}
			
			@Override
			public void onFailure(Throwable ble) {
				super.onFailure(ble);
				ToastUtil.showToastWithAlertPic("网络状况不佳，请稍后重试...");
			}
		};
		
		RequestParams params = new RequestParams();
		params.put("phonenumber", PreferencesUtils.getString(this, "username"));
		params.put("password", PreferencesUtils.getString(this, "password"));
		params.put("android_token", UmengRegistrar.getRegistrationId(this));//ios_token
		MgqRestClient.get("member_login", params, loginHandler);
	}
}
